﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using FinalSilverlightProject.Web;
using FinalSilverlightProject.Web.Models;
using FinalSilverlightProject.Web.Services;

namespace FinalSilverlightProject.Views.Login
{
    public partial class EditUserAccountWindow : ChildWindow
    {
        private readonly EditUserAcountContext _editUserAcountContext = new EditUserAcountContext();
        private EditRegistrationData _registrationData;

        public EditUserAccountWindow()
        {
            InitializeComponent();
            this.busyIndicator.IsBusy = true;
            InvokeOperation<EditRegistrationData> registrationData = _editUserAcountContext.GetUserRegistrationInfo(WebContext.Current.User.Name);
            registrationData.Completed += (s, e) =>
            {
                this.busyIndicator.IsBusy = false;
                if (!registrationData.HasError)
                {
                    _registrationData = registrationData.Value;
                    this.DataContext = _registrationData;
                }
            };
        }

        private void OkButtonClick(object sender, RoutedEventArgs e)
        {
            if (this.EditForm.ValidateItem())
            {
                this.busyIndicator.IsBusy = true;
                InvokeOperation op = _editUserAcountContext.UpdateUserRegistrationInfo(_registrationData);
                op.Completed += (s, ex) =>
                                    {
                                        this.busyIndicator.IsBusy = false;
                                        if (!op.HasError)
                                        {
                                            this.DialogResult = true;
                                            return;
                                        }
                                    };
            }
        }

        private void CancelButtonClick(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void EditForm_AutoGeneratingField(object sender, DataFormAutoGeneratingFieldEventArgs e)
        {
            e.Field.Mode = DataFieldMode.Edit;
            if (e.PropertyName == "UserName")
            {
                e.Field.IsReadOnly = true;
            }
        }

        /// <summary>
        /// Maps Esc to the cancel button and Enter to the OK button.
        /// </summary>
        private void EditFormKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                this.CancelButtonClick(sender, e);
            }
            else if (e.Key == Key.Enter && this.OKButton.IsEnabled)
            {
                this.OkButtonClick(sender, e);
            }
        }

        private void EditFormContentLoaded(object sender, DataFormContentLoadEventArgs e)
        {
            TextBox nameTextBox = ((DataForm)sender).FindName("FriendlyName") as TextBox;
            if (nameTextBox != null) nameTextBox.Focus();
        }

    }
}

